<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        window.onload=function(){
            /*
                点击按钮切换图片
            */
            var prev=document.getElementById("prev");
            var next=document.getElementById("next");
            var img=document.getElementsByTagName("img")[0];//返回的是数组
            //创建一个数组用来保存图片路径
            var imgarr=["./img/1.jpg","./img/2.jpg","./img/12.jpg"];
            //创建一个变量 保存当前正在显示的图片的索引
            var index=0;
            prev.onclick=function(){
                index--;
                if(index<0){
                    index=imgarr.length-1;
                }
                img.src=imgarr[index];
                
            }
            next.onclick=function(){
                index++;
                if(index>imgarr.length-1){
                    index=0;
                }
                img.src=imgarr[index];
            }
        }
    </script>
    <style>
        *{
            margin: 0;
            padding: 0;
            text-decoration: none;
        }
        #outer{
            width:450px;
            margin: 50px auto;
            padding: 10px;
            background-color: skyblue;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="outer">
        <img src="./img/1.jpg" alt="图1">
        <button id="prev">上一张</button>
        <button id="next">下一张</button>
    </div>
</body>
</html>